import * as survey from "../../api/survey";
import drawQrcode from 'weapp-qrcode-canvas-2d'

Page({
    data: {
        currentPage:1,
        createTime:[],
        keyword:"",
        list:[],
        currentPage:1,
        hasNextPage:true,
        total:"",
        tempFilePath:""
    },
    onLoad(){
        survey.getSurveyLink()
            .then((res)=>{
                this.create_qr_code( res.surveyLink );
            })
        this.get_survey_list();
    },
    create_qr_code(text){
        const query = wx.createSelectorQuery();
        const _this = this;
        query.select('#myQrcode')
        .fields({
            node: true,
            size: true
        })
        .exec((res) => {
            var canvas = res[0].node
            // 调用方法drawQrcode生成二维码
            drawQrcode({
                canvas: canvas,
                canvasId: 'myQrcode',
                width: 370,
                padding: 30,
                background: '#ffffff',
                foreground: '#000000',
                text: text,
            })
            // 获取临时路径（得到之后，想干嘛就干嘛了）
            wx.canvasToTempFilePath({
                canvasId: 'myQrcode',
                canvas: canvas,
                x: 0,
                y: 0,
                width: 370,
                height: 370,
                destWidth: 370,
                destHeight: 370,
                success(res) {
                    _this.setData({
                        tempFilePath:res.tempFilePath
                    })
                    console.log(res)
                },
                fail(res) {
                    console.error(res)
                }
            })
        })
    },
    get_survey_list(){

        let createTime =  this.data.createTime.length == 0 ? '' : this.data.createTime.join(",");

        survey.surveyCountList({
            currentPage:this.data.currentPage,
            pageSize:10,
            createTime:createTime
        })
        .then((res)=>{
            this.setData({
                list:this.data.currentPage == 1 ? res.list :[...this.data.list, ...res.list],
                hasNextPage:res.hasNextPage,
                total:res.total
            })
        })
    },
    top_picker_confirm(e){

        this.setData({
            list:[],
            hasNextPage:true,
            total:"",
            currentPage:1,
            createTime:e.detail
        })

        this.get_survey_list();
    },
    handle_survey_record(e){
        let { item } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/survey-record/survey-record?townCode='+item.townCode+"&countDate="+item.countDate+"&surveyNum="+item.surveyNum
        });
    },
    onReachBottom(){
        if(this.data.hasNextPage){
            this.setData({
                currentPage: ++this.data.currentPage
            })
            this.get_survey_list();
        }
    },
    // 保存图片
    saveImg: function (e) {

        let that = this;
        let imgSrc = this.data.tempFilePath;

        // wx.showModal({
        //     content: imgSrc
        // });

        wx.showLoading({
            title:"正在保存..."
        });
        //获取相册授权
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success() {
                            that.saveImageToPhotosAlbum(imgSrc);
                        },
                        fail() {
                            wx.hideLoading();
                        }
                    })
                } else {//用户已经授权过了
                    that.saveImageToPhotosAlbum(imgSrc);
                }
            }
        })

    },
    saveImageToPhotosAlbum(imgSrc){
        let fileName = new Date().valueOf();
        wx.saveImageToPhotosAlbum({
            filePath: imgSrc,
            success: function (data) {
                wx.showToast({
                    title: '保存成功!',
                })
            },
            fail: function (err) {
                if (err.errMsg === "saveImageToPhotosAlbum:fail cancel") {
                    wx.showToast({
                        title: '保存失败!',
                        icon: 'none'
                    })
                }
            },
        })
    },
    saveImgToLocal(imgSrc) {
        //var imgSrc = this.data.poster; //base64编码
        var save = wx.getFileSystemManager();
        var number = Math.random();

        wx.downloadFile({
            url: imgSrc,
            success: function (res) {
                //图片保存到本地
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function (data) {
                        wx.showToast({
                            title: '保存成功!',
                        })
                    },
                    fail: function (err) {
                        if (err.errMsg === "saveImageToPhotosAlbum:fail cancel") {
                            wx.showToast({
                                title: '保存失败!',
                                icon: 'none'
                            })
                        }
                    },
                })
            },
            fail(){
                wx.showToast({
                    title: '保存出错!',
                    icon: 'none'
                })
            }
        })
    }
})
